<?php

/**
 * job actions.
 *
 * @package    maximous
 * @subpackage job
 * @author     Mina.Gerges.Wadea
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class jobActions extends MaxControllerActions
{

    public function preExecute()
    {
        parent::preExecute();
        $this->maxUser->getMaxUserRole()->setDefaultSiteComponent(SiteComponent::Job);
    }

    public function executeIndex(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canView());
        $this->formFilter = new JobControllerFormFilter(array(), array('maxUser' => $this->maxUser));
        $this->formFilter->bind($request->getParameter($this->formFilter->getName()));
        $query    = $this->formFilter->buildQuery($this->formFilter->getValues());
        $page     = $request->getParameter('page', 1);
        $this->pager = $this->createPager('Job', $query, $page, sfConfig::get('app_table_list_row_numbers'));
    }

    public function executeNew()
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canAdd());
        $this->form = new JobForm(null, array('i18n'    => $this->i18n, 'maxUser' => $this->maxUser));
    }

    public function executeCreate(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canAdd());
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new JobForm(null, array('i18n'    => $this->i18n, 'maxUser' => $this->maxUser));
        $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));

        if ($this->form->isValid())
        {
            $job = $this->form->save();
            $this->getUser()->setFlash('message_success', $this->i18n->__('add job') . ' ' . $job->getTitle() . ' ' . $this->i18n->__('successful'));
            $this->redirect('job/index');
        }
        else
        {
            $this->getUser()->setFlash('message_failed', $this->i18n->__('add job') .  $this->i18n->__('failed'));
        }

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canEdit());
        $this->forward404Unless($job = Doctrine_Core::getTable('Job')->find(array($request->getParameter('id'))), sprintf('Object job does not exist (%s).', $request->getParameter('id')));
        $this->form = new JobForm($job, array('i18n'    => $this->i18n, 'maxUser' => $this->maxUser));
    }

    public function executeUpdate(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canEdit());
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($job = Doctrine_Core::getTable('Job')->find(array($request->getParameter('id'))), sprintf('Object job does not exist (%s).', $request->getParameter('id')));
        $this->form = new JobForm($job, array('i18n'    => $this->i18n, 'maxUser' => $this->maxUser));
        $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));

        if ($this->form->isValid())
        {
            $job = $this->form->save();
            $this->getUser()->setFlash('message_success', $this->i18n->__('edit job') . ' ' . $job->getTitle() . ' ' . $this->i18n->__('successful'));
        }
        else
        {
            $this->getUser()->setFlash('message_failed', $this->i18n->__('edit job') . ' ' . $job->getTitle() . ' ' . $this->i18n->__('failed'));
        }

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canDelete());
        $this->forward404Unless($job = Doctrine_Core::getTable('Job')->find(array($request->getParameter('id'))), sprintf('Object job does not exist (%s).', $request->getParameter('id')));
        $this->getUser()->setFlash('message_success', $this->i18n->__('delete job') . ' ' . $job->getTitle() . ' ' . $this->i18n->__('successful'));
        $job->delete();

        $this->redirect('job/index');
    }

}
